import { initPlugin } from "../backgroundUtils/initPlugin";
import { saveUrlLB } from "../backgroundUtils/backgroundLogicBusiness";
import { uploadAsset } from "@/backgroundUtils/upload-asset";
import { captureVisibleShortcut, captureVisibleScreenShot } from "@/backgroundUtils/captureVisibleShortcut";
import baseApi from "@/utils/apibase";
import { doAction } from "@/utils/chrome";

console.log("hello world background todo something~");

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  // 2. A page requested user data, respond with a copy of `user`
  console.log("收到消息", request);
  // if (request.action === 'keyDownEvent') {
  //     const screenshotUrl = await chrome.tabs.captureVisibleTab();
  //     console.log(screenshotUrl)
  //     console.log(request)
  // }

  switch (request.action) {
    case "keyDownEvent":
      // const screenshotUrl1 = await chrome.tabs.captureVisibleTab();
      console.log(screenshotUrl1);
      console.log(request);
      break;
    case "saveUrl":
      doAction(saveUrlLB, request.data).then(sendResponse);
      break;
    case "content-capture-visible-shortcut-api":
      doAction(captureVisibleShortcut).then(sendResponse);
      break;
    case "upload-asset-api":
      doAction(uploadAsset, request.data).then(sendResponse);
      break;
    case "request-api":
      const data = request.data;
      const { url } = data;
      doAction(baseApi, { ...data, url: "/api" + url }).then(sendResponse);
      break;
    case "screen-shot-api":
      captureVisibleScreenShot().then(sendResponse);
      break;
    default:
      console.log("未匹配到action");
      break;
  }
  return true;
});

// 执行初始化插件后触发的回调函数
function initPluginCallBack(flag) {
  // flag : 自动登录成功的标识
  console.log("初始化插件后触发的回调函数", flag);
}

//初始化插件信息
initPlugin(initPluginCallBack);
